<template>
  <el-form ref="form" :model="form" label-width="100px" :rules="rules">
    <el-form-item label="dhcp" prop="dhcp">
      <el-select v-model="form.dhcp">
        <el-option 
          v-for="item in SYS_PARAMS.dhcp"
          :key="item.id"
          :label="item.name"
          :value="item.code"
        ></el-option>
      </el-select>
    </el-form-item>
    
    <el-form-item label="IP地址" prop="ip">
      <el-input v-model="form.ip"></el-input>
    </el-form-item>

    <el-form-item label="子网掩码" prop="nmk">
      <el-input v-model="form.nmk"></el-input>
    </el-form-item>

    <el-form-item label="网关IP" prop="gw">
      <el-input v-model="form.gw"></el-input>
    </el-form-item>
    <el-form-item label="dns1" prop="dns1">
      <el-input v-model="form.dns1" ></el-input>
    </el-form-item>
    <el-form-item label="dns2" prop="dns2">
      <el-input v-model="form.dns2" ></el-input>
    </el-form-item>

     <div class="form-button">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
    </div>
  </el-form>
</template>
<script>
import { mapGetters } from 'vuex'

import {
  getIpConfigApi,
  updateIpConfigApi
} from '@/api/ip'

export default {
  name: 'form-ip',
  props:{
    data: Object
  },
  computed: {
    ...mapGetters(['SYS_PARAMS'])
  },
  data(){
    return {
      form: {
        dhcp: 0,
        ip: '',
        nmk: '',
        gw: '',
        dns1: '',
        dns2: '',
        mac: ''
      },
      rules:{
        dhcp:[{required: true, trigger: 'blur', message: '请选择dhcp'}],
        ip:[{required: true, trigger: 'blur', message: '请输入IP地址'}],
        gw:[{required: true, trigger: 'blur', message: '请输入网关IP地址'}],
        nmk:[{required: true, trigger: 'blur', message: '请输入子网掩码'}],
        dns1:[{required: true, trigger: 'blur', message: '请输入dns1'}],
      },
      loading: false
    }
  },
  methods: {
    cancel(){
      this.$emit('close')
    },
    confirm(){
      this.$refs.form.validate( valid => {
        if(valid){
          this.loading = true;
          this.updateIpconfig();
        }
      })
    },
    getIpConfig(){
      this.form = {}
      getIpConfigApi().then(res => {
        if(res.code ==0){
          this.form = res.data || {};
          console.log(this.form)
        }
      })
    },
    updateIpconfig(){
      updateIpConfigApi(this.form).then(res => {
        this.loading = false;
        if(res.code == 0){
          this.$message({type: 'success', message: '提交成功'})
          this.$emit('close')
        }
      })
    
    }
  }

}
</script>
<style  scoped>
.form-button{
  margin-left: 30px;
  position: absolute;
  bottom: 20px;
}
</style>